package dimension.starry.block;

import dimension.starry.StarryDimension;
import dimension.starry.block.dungeon.HealJar;
import dimension.starry.block.dungeon.MagicJar;
import dimension.starry.block.loot.*;
import dimension.starry.block.ore.AnthraciteOre;
import dimension.starry.block.ore.SaltOre;
import dimension.starry.block.ore.SiliconOre;
import dimension.starry.block.ore.TinOre;
import dimension.starry.item.ItemGroups;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.feature.PlacedFeature;

import java.util.Arrays;

/**
 * @author 150149
 */
public enum BlockRegistryManager {

    SALT_ORE("salt_ore", new SaltOre()),
    TIN_ORE("tin_ore", new TinOre()),
    ANTHRACITE_ORE("anthracite_ore", new AnthraciteOre()),
    SILICON_ORE("silicon_ore", new SiliconOre()),
    NORMAL_LOOT_CHEST("normal_loot_chest", new NormalLootChest()),
    FISH_LOOT_CHEST("fish_loot_chest", new FishLootChest()),
    PASTURE_LOOT_CHEST("pasture_loot_chest", new PastureLootChest()),
    BOOK_LOOT_CHEST("book_loot_chest", new BookLootChest()),
    FARM_LOOT_CHEST("farm_loot_chest", new FarmLootChest()),
    POTION_LOOT_CHEST("potion_loot_chest", new PotionLootChest()),
    FOOD_LOOT_CHEST("food_loot_chest", new FoodLootChest()),
    HEAL_JAR("heal_jar", new HealJar()),
    MAGIC_JAR("magic_jar", new MagicJar()),
    ;

    private final String name;
    private final Block block;
    private final BlockItem blockItem;

    BlockRegistryManager(String name, Block block) {
        this.name = name;
        this.block = block;
        this.blockItem = new BlockItem(block, new FabricItemSettings());
    }

    public static void onInitialize() {
        Arrays.stream(BlockRegistryManager.values()).peek(blockEnum -> {
            Registry.register(Registries.BLOCK, new Identifier(StarryDimension.MOD_ID, blockEnum.getName()), blockEnum.getBlock());
            Registry.register(Registries.ITEM, new Identifier(StarryDimension.MOD_ID, blockEnum.getName()), blockEnum.getBlockItem());
            ItemGroups.registerToGroup(blockEnum.getBlockItem());
        }).filter(blockEnum -> blockEnum.getName().endsWith("_ore")).forEach(blockEnum -> {
            RegistryKey<PlacedFeature> registryKey = RegistryKey.of(RegistryKeys.PLACED_FEATURE,
                new Identifier(StarryDimension.MOD_ID, blockEnum.getName())
            );
            BiomeModifications.addFeature(BiomeSelectors.foundInOverworld(), GenerationStep.Feature.UNDERGROUND_ORES, registryKey);
        });
    }

    public String getName() {
        return name;
    }

    public Block getBlock() {
        return block;
    }

    public BlockItem getBlockItem() {
        return blockItem;
    }
}
